﻿using System;
using System.Windows;
using Phoenix.Frames;
using Phoenix.Framework;

namespace Phoenix.ActionResults
{
    public class MessageBoxActionResult : ActionResult, IHaveInvokeCompletedSteps
    {
        readonly Action<MessageBoxCallback> resultCallback;
        readonly Func<MessageBoxResult> show;

        public MessageBoxActionResult(ControllerActionContext context, string messageBoxText, Action<MessageBoxCallback> resultCallback = null)
            : base(context)
        {
            this.resultCallback = resultCallback;
            show = () => MessageBox.Show(GetOwner(context), messageBoxText);
        }

        public MessageBoxActionResult(ControllerActionContext context, string messageBoxText, string caption, Action<MessageBoxCallback> resultCallback = null)
            : base(context)
        {
            this.resultCallback = resultCallback;
            show = () => MessageBox.Show(GetOwner(context), messageBoxText, caption);
        }

        public MessageBoxActionResult(
            ControllerActionContext context, string messageBoxText, string caption,
            MessageBoxButton button, Action<MessageBoxCallback> resultCallback = null)
            : base(context)
        {
            this.resultCallback = resultCallback;
            show = () => MessageBox.Show(GetOwner(context), messageBoxText, caption, button);
        }

        public MessageBoxActionResult(
            ControllerActionContext context, string messageBoxText, string caption,
            MessageBoxButton button, MessageBoxImage icon, Action<MessageBoxCallback> resultCallback = null)
            : base(context)
        {
            this.resultCallback = resultCallback;
            show = () => MessageBox.Show(GetOwner(context), messageBoxText, caption, button, icon);
        }

        public MessageBoxActionResult(
            ControllerActionContext context, string messageBoxText, string caption,
            MessageBoxButton button, MessageBoxImage icon, MessageBoxResult defaultResult, Action<MessageBoxCallback> resultCallback = null)
            : base(context)
        {
            this.resultCallback = resultCallback;
            show = () => MessageBox.Show(GetOwner(context), messageBoxText, caption, button, icon, defaultResult);
        }

        public MessageBoxActionResult(
            ControllerActionContext context, string messageBoxText, string caption,
            MessageBoxButton button, MessageBoxImage icon, MessageBoxResult defaultResult,
            MessageBoxOptions options, Action<MessageBoxCallback> resultCallback = null)
            : base(context)
        {
            this.resultCallback = resultCallback;
            show = () => MessageBox.Show(GetOwner(context), messageBoxText, caption, button, icon, defaultResult, options);
        }

        public override void Execute(IContentFrame targetFrame)
        {
        }

        static Window GetOwner(ControllerActionContext context)
        {
            return context.Host.HostTarget.ContentHost as Window;
        }

        public void InvokeComplete()
        {
            var result = show();
            if (resultCallback != null)
                resultCallback(new MessageBoxCallback(result, Context));
        }
    }
}